package configuration;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author artem
 */
public class PropertiesHandler {

    Properties properties;
    String filename;
    String relativeConfigurationPath = "src/configuration/";

    public PropertiesHandler(String filename) {
        properties = new Properties();
        this.filename = filename;
    }

    private void open(String filename) {
        try {
            properties.load(new FileInputStream(relativeConfigurationPath + filename + ".properties"));
        } catch (IOException e) {
            System.out.println("ERROR:  Error reading file \"" + filename + ".properties\"");
        }
    }

    private void close(String filename) {
        try {
            properties.store(new FileOutputStream(relativeConfigurationPath + filename + ".properties"), null);
        } catch (IOException e) {
            System.out.println("ERROR:  Error closing file \"" + filename + ".properties\"");
        }
    }

    public int getInt(String keyName) {
        int ret = 0;
        open(filename);
        String tmp = properties.getProperty(keyName);
        ret = Integer.valueOf(tmp);
        close(filename);
        return ret;
    }

    public void setInt(String keyName, int value) {
        open(filename);
        properties.setProperty(keyName, String.valueOf(value));
        close(filename);
    }

    public double getDouble(String keyName) {
        double ret = 0;
        open(filename);
        String tmp = properties.getProperty(keyName);
        ret = Double.valueOf(tmp);
        close(filename);
        return ret;
    }

    public void setDouble(String keyName, double value) {
        open(filename);
        properties.setProperty(keyName, String.valueOf(value));
        close(filename);
    }

    public String getString(String keyName) {
        String ret = null;
        open(filename);
        ret = properties.getProperty(keyName);
        close(filename);
        return ret;        
    }

    public void setString(String keyName, String value) {
        open(filename);
        properties.setProperty(keyName, value);
    }
}
